/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: main.cpp
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#include <codec/python_codec.h>
#include <codec/json_codec.h>
#include <codec/c_codec.h>
#include <tools/debug.h>
#include <stdio.h>

PyObject *Get_PyFunc(PyObject *pyModule, const char *szFuncName)
{
	PyObject* pDict = PyModule_GetDict(pyModule);
	if (!pDict) {
		return NULL;
	}
	return PyDict_GetItem(pDict, Py_BuildValue("s", szFuncName));
}

int pyencodec_test()
{
	Py_Initialize();
	if (!Py_IsInitialized())
		return -1;

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./scripts')");

	PyObject *pModule = PyImport_ImportModule("sample.packet");
	if (!pModule) {
		printf("load python module fail!\n");
		return -1;
	}

	PyObject* pDict = PyModule_GetDict(pModule);
	if (!pDict) {
		printf("Cant find dictionary.\n");
		return -1;
	}
	PyObject *test_print = Get_PyFunc(pModule, "test_print");

	PyObject *test_start = Get_PyFunc(pModule, "start_test");
	PyObject *test_end = Get_PyFunc(pModule, "end_test");

	char szTempBuf[__BUFFER_SIZE__];
	BUFFER_WRAPPER buff;
	buff.init(szTempBuf, __BUFFER_SIZE__);

	CPythonCodec pyCodec;
	CCCodec c;
	IEncoder *pE = c.GetEncoder();
	IDecoder *pD = c.GetDecoder();
	IEncoder *pyE = pyCodec.GetEncoder();
	IDecoder *pyD = pyCodec.GetDecoder();

	PyObject *pyDst = Get_PyFunc(pModule, "DICT_VAR");

	CMapWrapper mapwrapper;
	CListWrapper listwrapper;
	CListWrapper listwrapper2;

	int ok_c = 0;
	int fail_c = 0;

	const char *testdata = "{21: 'abc\\\"\\'def', 22: 2,'empty':false, 'sub-dict': {44: {253: null, 254: 20001,25: {253: null, 254: 20001,26: {253: null, 254: 20001}}}, 253: [1,2,3,4,5,null,{'id':'inner'}], 24: 20001}};[12,23]";//{22:33, 33:{101:2, 102:0}}}";
	//const char *testdata = "{1:0x110,2:null}";

	CJsonCodec jsonCodec;
	unsigned long dwT = GetTimestamp();
	PyObject_CallFunction(test_start,"");
	//for ( int i = 0; i < 10000; i ++)
	{
		for ( int i = 0; i < 100000; i ++)
		{
			JSON_BUFFER jb = {testdata, testdata + strlen(testdata)};
			buff.reset();
			jsonCodec.GetEncoder()->Encode(&buff, &jb);
			buff.forward();
			JSON_BUFFER *b = (JSON_BUFFER *)jsonCodec.GetDecoder()->Decode(&buff, &jb);
			//printf(b->begin);
			if(strlen(b->begin) == 166) ok_c ++;
		}
		//pyE->Encode(&buff, pyDst);
		//pyD->Decode(&buff, pyDst);
	}
	PyObject_CallFunction(test_print,"");
	printf("ok: %d, fail: %d, C-TIME: %ld (ms)\n", ok_c, 0, GetTimestamp() - dwT);
	PyObject_CallFunction(test_end,"");
	return 0;

	PyObject_CallFunction(test_start,"");
	dwT = GetTimestamp();
	for (int i = 0; i < 100000; i ++)
	{
		//if (i >= 10000 && i % 10000 == 0)
		//{
		//	//printf("10000...\n");
		//}
		//buff.reset();
		//pyE->Encode(&buff, pyDst);

		PyDict_Clear(pyDst);

		listwrapper.Reset();
		listwrapper2.Reset();
		mapwrapper.Reset();

		listwrapper.Add<int>(10012);
		listwrapper.Add<float>(1.314f);
		listwrapper.Add<int>(21015);
		listwrapper.Add<bool>(true);
		listwrapper.Add<INT64>(1314);
		listwrapper.Add<ENCODESTR>("test sub list ok?");

		listwrapper2.Add<ENCODESTR>("test list ok?");
		listwrapper2.Add<bool>(false);
		listwrapper2.Add<LPBUFFER_WRAPPER>(listwrapper.GetBuffer());
		listwrapper2.Add<int>(20013);

		mapwrapper.Set<int, ENCODESTR>(101, "your sister!");
		mapwrapper.Set<float, ENCODESTR>(1.314f, "rate!");
		mapwrapper.Set<ENCODESTR, int>("rate!", 65534);
		mapwrapper.Set<ENCODESTR, LPBUFFER_WRAPPER>("list1", listwrapper2.GetBuffer());

		pyD->Decode(mapwrapper.GetBuffer(), pyDst);
		ok_c ++;
	}
	printf("ok: %d, fail: %d, C-TIME: %ld (ms)\n", ok_c, fail_c, GetTimestamp() - dwT);
	PyObject_CallFunction(test_end,"");

	Py_Finalize();
	return 0;
}

int main()
{
	int r = pyencodec_test();
	getchar();
	return r;
}












